package yxy.game.pm2.active.command.sdk.weixin.heroshare;

import yxy.apple.logger.Logger;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.herodata;
import yxy.cherry.data.bean.packagedata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.sdk.weixin.ActiveModule_武将分享;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.message.SdkActiveMessage;
import yxy.game.pm2.message.SdkActiveMessage.WeiXinHeroShareDrawRewardRequest;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;

/**
 * 在线礼包
 * @author Dell
 *
 */
@MessageType(proto = Command.WeiXinHeroShareDrawReward, classtype = SdkActiveMessage.WeiXinHeroShareDrawRewardRequest.class)
public class CMD_Active_武将分享_DrawReward extends Command<WeiXinHeroShareDrawRewardRequest> {
	
	private static final Logger logger = Logger.create(CMD_Active_武将分享_DrawReward.class);
	
	@Override
	public void execute(MessageSource messageSource, WeiXinHeroShareDrawRewardRequest request) throws Exception {
		String pcode = messageSource.getTag();
		int heroid = request.getHeroid();
		SdkActiveMessage.WeiXinHeroShareDrawRewardResponse.Builder response = SdkActiveMessage.WeiXinHeroShareDrawRewardResponse.newBuilder();
		
		ActiveModule_武将分享 am = ActiveModule_武将分享.get(pcode);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
				
		if(am.getShareheroids().contains(heroid)) {
			send(messageSource, response, Result.ActHadGet);
			return;
		}
	
		herodata data = DataCenter.getData(heroid, herodata.class);
		if(data == null) {
			send(messageSource, response, Result.DataError);
			return;
		}
		
		if(data.getStartcs() != 5) {
			send(messageSource, response, Result.ActCannotGet);
			return;
		}

		am.getShareheroids().add(heroid);
		am.set();
		
		packagedata pd = DataCenter.getData(40, packagedata.class);
		
		Collection<Currency> curs = Currency.create(pd.getRewardtype(), pd.getRewardid(), pd.getRewardnum());
		Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
		response.addAllDisplay(items);
		
		new CurrencyHandle().addCurrency(pcode, CCReason.微信_活动_武将分享, curs);
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, SdkActiveMessage.WeiXinHeroShareDrawRewardResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
	
}
